package birdry.ui.components.navigation
{
	
	import flash.events.MouseEvent;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	/**
	* ...
	* 
		var Navigation:AlphaMotionButtonGroup = new AlphaMotionButtonGroup( [bt1, bt2, bt3] );
		Navigation.clickListener = onBtClick;
		function onBtClick(num:uint):void 		{
			trace( "num : " + num );
		}
	* @author 
	*/
	
	public class Navigation 	{
		
		protected var _max:uint;
		protected var _btArr:Array;
		private var _clickListener:Function;
		private var _selectedBt:*;
		private var _interalID:uint;
		private var _outInterval:uint = 500;
		private var _activeMotion:IMotionDefine;
		
		/**
		 * 버튼 아웃시 이벤트를 발생할시간 설정값
		 */
		public function get outInterval(): uint				{			return _outInterval;		}
		public function set outInterval(time:uint): void	{			_outInterval = time;		}
		
		/**
		 * 버튼 클릭시 이벤트리스너 함수지정
		 */
		public function set clickListener(listener:Function): void	{		_clickListener = listener;		}
		
		public function select(num:uint): void	{
			_selectedBt = _btArr[num];
			_activeMotion.active(_selectedBt);
		}
		
		public function Navigation(btArr:Array, motion:IMotionDefine = null )	{
			_max = btArr.length;
			_btArr = btArr;
			activeMotion = (motion == null) ? new FrameMotion() : motion;
			addEvent();
		}
		
		private function addEvent():void 		{
			for (var i:uint = 0; i < _max; ++i)	{
				_btArr[i].buttonMode = true;
				_btArr[i].addEventListener(MouseEvent.MOUSE_OVER, onBtOver);
				_btArr[i].addEventListener(MouseEvent.MOUSE_OUT, onBtOut);
				_btArr[i].addEventListener(MouseEvent.CLICK, onBtClick);
			}
		}
		
		private function set activeMotion(motion:IMotionDefine): void	{
			if (_activeMotion)	_activeMotion.destroy();
			_activeMotion = motion;
			_activeMotion.max = _max;
			_activeMotion.btArr = _btArr;
		}
		
		private function onBtOver(e:MouseEvent):void 		{
			clearTimeout(_interalID);
			_activeMotion.active(e.currentTarget);
		}
		
		private function onBtOut(e:MouseEvent):void 		{
			_interalID = setTimeout( function()	{
				_activeMotion.active(_selectedBt);
			}, _outInterval);
		}
		
		private function onBtClick(e:MouseEvent):void 		{
			var num:uint = _btArr.indexOf(e.currentTarget);
			if(_clickListener != null)	_clickListener(num);
		}
		
		
	}	// class
	
}	// package